<?php
/* +----------------------------------------------------------------------
// | 	xaoam.com
// |	author:am
// |	gitee:https://gitee.com/xaoam/amAdmin
// |	email:hi@xaoam.com
// |	
// +----------------------------------------------------------------------*/
namespace app\admin\controller;
use think\Controller;
use think\Db;
use Casbin;

class User extends Base {
    public function initialize(){
        parent::initialize();
		$this->model = new \app\admin\model\UserM;
    }

    public function index(){
		$this->assign('dataUrl',url('user/lists'));
        return $this->fetch();
    }
	public function lists($page=1,$limit=10){
		$list = $this->model->lists($limit);
		return json(['code'=>0,'data'=>$list->items(),'page'=>$page,'limit'=>$limit,'count'=>$list->total()]);
	}
	public function cue(){
		$data = request()->param();
		$res = $this->model->cue($data);
		if($res){
			$this->reJson['code'] = 200;
			$this->reJson['msg']= '操作成功!';
		}
		return json($this->reJson);
	}
	public function add(){
		$data = request()->param();
		if(request()->isPost()){
			$falg = $this->model->find(['username'=>$data['username']]);
			if($falg){
				$this->reJson['msg']= '用户已经存在!';
			}else{
				$res = $this->model->add($data);
				if($res){
					$this->addBasicRule($res['id']);
					$this->reJson['code'] = 200;
					$this->reJson['msg']= '操作成功!';
				}
			}
			return json($this->reJson);
		}else{
			return $this->fetch();
		}
	}
	public function edit(){
		$data = request()->param();
		if(request()->isPost()){
					$res = $this->model->edit($data);
					if($res){
						$this->reJson['code'] = 200;
						$this->reJson['msg']= '操作成功!';
						if(isset($data['passwd']) && $this->user_info['id'] == $data['id']){
							/*当前登陆用户编辑密码,跳转到登陆页*/
							session(null);
							$this->error('重新登陆',url('login/index'),'reload',302);
						}
					}elseif($res === NULL){
						$this->reJson['msg']= '用户已经存在!';
					}else{
						
					}
				return json($this->reJson);
		}else{
			$id = request()->param('id');
			if($id){
				$this->assign('d_json',json_encode(Db::name('user')->where('id',$id)->field('passwd,salt',true)->find()));
				$this->assign('isRoot',$this->user_info['id'] == 1 ?1:0);
				return $this->fetch();				
			}else{
				return $this->fetch('user/add');
			}
		}		
	}
	public function del(){
			$data = request()->param();
			if($data['id'] == $this->user_info['id']){
				$this->reJson['msg']= '登陆中账号/默认管理员无法删除!';
			}else{
				$res = $this->model->dels($data);
				if($res){
					Casbin::DeleteRolesForUser($data['id']);
					$this->reJson['code'] = 200;
					$this->reJson['msg']= '操作成功!';
				}
			}
			return json($this->reJson);
		}
	public function dels(){
		$data = request()->param();
		if(in_array($this->user_info['id'],$data['ids'])){
			$this->reJson['msg']= '登陆中账号/默认管理员无法删除!';
		}else{
			$res = $this->model->dels($data['ids']);
				if($res){
					foreach($data['ids'] as $row){
						Casbin::DeleteRolesForUser($row);
					}
					$this->reJson['code'] = 200;
					$this->reJson['msg']= '操作成功!';
				}
		}
		return json($this->reJson);
	}


}
